//
//  BIZOfflineConsultQuestionDetailVC.m
//  biz
//
//  Created by zhangfuwei on 2017/4/17.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZOfflineConsultQuestionDetailVC.h"
#import "BIZOfflineConsultRecordsModel.h"




@interface BIZOfflineConsultQuestionDetailVC ()
/** 咨询问题详情 */
@property (nonatomic, strong) BIZOfflineConsultRecordsDataModel *detailModel;
/** 咨询问题uuid */
@property (nonatomic, copy) NSString *uuid;
/** scrollView主视图 */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 顶部子视图 */
@property (nonatomic, strong) UIView *topSubView;
/** 底部子视图 */
@property (nonatomic, strong) UIView *bottomSubView;
/** 头像 */
@property (nonatomic, strong)   UIImageView *headImageView;
/** 名称 */
@property (nonatomic, strong)   UILabel *nameLabel;
/** 内容 */
@property (nonatomic, strong)   UILabel *contentLabel;
/**  服务起止时间 */
@property (nonatomic, strong)   UILabel *severLabel;
/**服务创建时间 */
@property (nonatomic, strong)   UILabel *createTimeLabel;
/**  状态 */
@property (nonatomic, strong)   UILabel *stateLabel;
/**  状态 */
@property (nonatomic, strong)   UIButton *serviceFinishBtn;
/** 分割线 */
@property (nonatomic, strong)   UIView  *splitLine;


@end

@implementation BIZOfflineConsultQuestionDetailVC

//-(instancetype)initWithQuestionDetailModel:(BIZOfflineConsultRecordsDataModel *)detailModel {
//    self = [super init];
//    if (self) {
//        _detailModel = detailModel;
//    }
//    return self;
//}

- (instancetype)initWithQuestionUuid:(NSString *)uuid {
    self = [super init];
    if (self) {
        _uuid = uuid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupConfig];
//    if (_detailModel) {
//        [self loadUI];
//        
//    }else if (_uuid) {
//        [self requestQuestionDetailWithUUID:_uuid];
//    }
    [self requestQuestionDetailWithUUID:_uuid];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 视图初始化

- (void)setupConfig {
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 62, 15)];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.font = CHINESE_SYSTEM(16);
    titleLab.text = @"问题咨询";
    self.navigationItem.titleView = titleLab;

    self.view.backgroundColor = HEXCOLOR(0xf9f9f9);
    
}

- (void)loadUI {
    //  视图间距
    CGFloat kViewMargin = 15;
    CGFloat kMargin     = 10;
    CGFloat imgW        = 50;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-BIZNavgationBarH)];
    _scrollView.backgroundColor = HEXCOLOR(0xf9f9f9);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.alwaysBounceVertical = NO;
    [self.view addSubview:_scrollView];
    
    //  顶部/底部子视图
    _topSubView = [[UIView alloc] init];
    _topSubView.backgroundColor = [UIColor whiteColor];
    [_topSubView.layer setBorderColor:HEXCOLOR(0xeeedee).CGColor];
    [_topSubView.layer setBorderWidth:1];

    
    _bottomSubView = [[UIView alloc] init];
    _bottomSubView.backgroundColor = [UIColor whiteColor];
    [_bottomSubView.layer setBorderColor:HEXCOLOR(0xeeedee).CGColor];
    [_bottomSubView.layer setBorderWidth:1];
    
    [_scrollView addSubview:_topSubView];
    [_scrollView addSubview:_bottomSubView];
    
    [_topSubView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView).offset(kMargin);
        make.left.right.equalTo(self.view);
    }];

    [_bottomSubView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topSubView.mas_bottom).offset(kMargin);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@126);
        make.bottom.equalTo(_scrollView);
    }];
    
    
    // 顶部视图的子控件
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kViewMargin, 8, imgW, imgW)];
    _headImageView.layer.cornerRadius = 5;
    _headImageView.clipsToBounds = YES;
    
    _nameLabel          = [[UILabel alloc] init];
    _contentLabel       = [[UILabel alloc] init];
    _severLabel         = [[UILabel alloc] init];
    _createTimeLabel    = [[UILabel alloc] init];
    _stateLabel         = [[UILabel alloc] init];
    _serviceFinishBtn   = [[UIButton alloc] init];

    
    _contentLabel.numberOfLines     = 0;
    [_serviceFinishBtn.layer setBorderColor:HEXCOLOR(0x797979).CGColor];
    [_serviceFinishBtn.layer setBorderWidth:1];
    
    [_createTimeLabel.layer setBorderColor:HEXCOLOR(0xf0f0f0).CGColor];
    [_createTimeLabel.layer setBorderWidth:0.5];
    
    _nameLabel.font         = SYSTEMFONT(14);
    _contentLabel.font      = SYSTEMFONT(13);
    _severLabel.font         = SYSTEMFONT(14);
    _createTimeLabel.font      = SYSTEMFONT(13);
    _stateLabel.font      = SYSTEMFONT(13);
    _serviceFinishBtn.titleLabel.font      = SYSTEMFONT(13);
    
    _nameLabel.textColor    = HEXCOLOR(0x4c5c6c);
    _contentLabel.textColor = HEXCOLOR(0x010101);
    _createTimeLabel.textColor = HEXCOLOR(0xb7b7b7);
    
    _splitLine = [[UIView alloc] init];
    _splitLine.backgroundColor = HEXCOLOR(0xeeedee);

    
    [_serviceFinishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_serviceFinishBtn addTarget:self action:@selector(serviceFinishBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_serviceFinishBtn setTitle:@"服务完成" forState:UIControlStateNormal];
    _serviceFinishBtn.backgroundColor = COLOR_NAVBAR_BLUE;
    _serviceFinishBtn.layer.cornerRadius = 25;
    _serviceFinishBtn.layer.masksToBounds = YES;
    
    //  添加子控件
    [_topSubView addSubview:_headImageView];
    [_topSubView addSubview:_nameLabel];
    [_topSubView addSubview:_contentLabel];
    [_topSubView addSubview:_createTimeLabel];
    [_topSubView addSubview:_stateLabel];
    [_topSubView addSubview:_splitLine];
    
    [_bottomSubView addSubview:_severLabel];
    [_bottomSubView addSubview:_serviceFinishBtn];

    //  布局
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headImageView.mas_right).offset(12);
        make.centerY.equalTo(_headImageView);
        make.height.equalTo(@(15));
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topSubView).offset(66);
        make.left.equalTo(_topSubView).offset(kViewMargin);
        make.right.equalTo(_topSubView).offset(-1*kViewMargin);
    }];
    
    [_splitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topSubView).offset(kViewMargin);
        make.right.equalTo(_topSubView).offset(-1*kViewMargin);
        make.top.equalTo(_contentLabel.mas_bottom).offset(12);
        make.height.equalTo(@0.5);
    }];
    
    [_createTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_splitLine.mas_bottom).offset(17);
        make.left.equalTo(_splitLine);
        make.height.equalTo(@12);
        make.bottom.equalTo(_topSubView).offset(-1*kViewMargin);
    }];
    
    [_stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_splitLine);
        make.centerY.equalTo(_createTimeLabel);
        make.height.equalTo(@13);
    }];
    
    [_severLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomSubView).offset(kViewMargin);
        make.left.equalTo(_bottomSubView).offset(kViewMargin);
        make.height.equalTo(@13);
    }];

    [_serviceFinishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_severLabel.mas_bottom).offset(kViewMargin);
        make.height.equalTo(@50);
        make.left.equalTo(_bottomSubView).offset(kViewMargin);
        make.right.equalTo(_bottomSubView).offset(-1*kViewMargin);
    }];
    
    // 数据赋值
    NSString    *state ;
    UIColor     *stateColor;
    BOOL        finishBtnHidden = YES;
    switch (_detailModel.status.intValue) {
        case 0:
            state = @"咨询师未接单";
            stateColor = HEXCOLOR(0xfb6a31);
            break;
        case 1:
            state = @"咨询师已接单";
            stateColor = HEXCOLOR(0x00c44e);
            finishBtnHidden = NO;
            break;
        case 2:
            state = @"咨询师拒单";
            stateColor = HEXCOLOR(0x2079ef);
            
            break;
        case 3:
            state = @"服务完成";
            stateColor = HEXCOLOR(0xffa200);
            break;
        case 4:
            state = @"服务无响应";
            stateColor = HEXCOLOR(0xcc100f);
            break;
        default:
            break;
    }
    _stateLabel.text = state;
    _stateLabel.textColor = stateColor;
    _serviceFinishBtn.hidden = finishBtnHidden;
    if (finishBtnHidden) {
        
        [_bottomSubView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@45);
        }];
    }
    
    [SDWebImageTool imageView:_headImageView imageName:_detailModel.headImg placeHolder:@"default-head" showProgress:NO];
    
    
    _nameLabel.text = _detailModel.cname;
    _contentLabel.text = _detailModel.content;
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"服务时间: %@ - %@",[NSDate stringFromTimestamp:_detailModel.updateTime dateFormat:@"yyyy MM dd"],[NSDate stringFromTimestamp:_detailModel.endTime dateFormat:@"yyyy MM dd"]] attributes:@{NSForegroundColorAttributeName:COLOR_NAVBAR_BLUE,NSFontAttributeName:CHINESE_SYSTEM(13)}];
    
    [attStr addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, 4)];
    
    _severLabel.attributedText = attStr;

    _createTimeLabel.text = [NSString stringWithFormat:@"%@",[NSDate stringFromTimestamp:_detailModel.updateTime dateFormat:@"yyyy-MM-dd HH:mm"]];
   
}



#pragma mark -- 网络请求
- (void)requestServiceDoneSubmitionWithOfflineConsultID:(NSString *)uuid {
    
    [RequestTool POST:kOfflineConsultServiceDoneURL
           parameters:@{@"uuid":uuid?:@""}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"确认服务已完成--%@",responseObject);
                  NSString *code = responseObject[@"code"];
                  NSString *des = responseObject[@"des"];
                  if (code.intValue == 1) {
                    
                      _serviceFinishBtn.hidden = YES;
                      _stateLabel.text = @"服务完成";;
                      _stateLabel.textColor = HEXCOLOR(0xffa200);

                      [_bottomSubView mas_updateConstraints:^(MASConstraintMaker *make) {
                          make.height.equalTo(@45);
                      }];
                  }else if (code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];

                  }else {
                      [MBProgressHUD showErrorMessage:des];

                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
    
}

- (void)requestQuestionDetailWithUUID:(NSString *)uuid {
    [MBProgressHUD showActivityMessageInView:@"数据加载中..."];
    if (uuid == nil) {
        [MBProgressHUD showErrorMessage:@"请求参数有误"];
        return;
    }
    
    [RequestTool POST:kOfflineConsultQuestionDetailURL
           parameters:@{@"uuid":uuid}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"问题详情--%@",responseObject);
                  NSString *code = responseObject[@"code"];
                  NSString *des = responseObject[@"des"];
                  NSDictionary *data = responseObject[@"data"];
                  
                  if (code.intValue == 1) {
                      [MBProgressHUD showSuccessMessage:des];
                      if ([data isKindOfClass:[NSDictionary class]]) {
                          
                          _detailModel = [BIZOfflineConsultRecordsDataModel yy_modelWithDictionary:data];
                          [self loadUI];
                      }
                      
                  }else if (code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:des];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}


#pragma mark -- private methods
-(void)setDetailModel:(BIZOfflineConsultRecordsDataModel *)detailModel {
    _detailModel = detailModel;
    
    [SDWebImageTool imageView:_headImageView imageName:detailModel.headImg placeHolder:@"default-head" showProgress:NO];
    
    _nameLabel.text = detailModel.cname;
    _contentLabel.text = detailModel.content;
}

- (void)serviceFinishBtnClick:(UIButton *)btn {
    
    [self requestServiceDoneSubmitionWithOfflineConsultID:_detailModel.uuid];
    
}


@end
